package cn.edu.sjtu.stap.cfg.internal;

import java.util.*;

// import org.aspectj.ajdt.internal.compiler.*;
import org.aspectj.asm.AsmManager;
import org.aspectj.ajdt.internal.core.builder.*;
import org.aspectj.org.eclipse.jdt.internal.compiler.ast.*;

import cn.edu.sjtu.stap.cg.*;
import cn.edu.sjtu.stap.cg.builder.*;
import cn.edu.sjtu.stap.hg.builder.*;
import cn.edu.sjtu.stap.util.*;

public privileged aspect CgBuilderAdapter {
//    declare precedence : InterSdgBuilderAdapter,CgBuilderAdapter;

    after(AjBuildConfig b) : AsmFetcher.asmFetcherPc(b) {
        try {
            ICgBuilder cgBuilder = Initializer.getCgBuilder();
            IHgBuilder hgBuilder = Initializer.getHgBuilder();

            // Add common calls.
            Iterator<CompilationUnitDeclaration> unitIt = AstArray.getInstance().iterator();
            while (unitIt.hasNext()) {
                cgBuilder.buildCallGraph(hgBuilder.getResult(), unitIt.next());
            }

            // Add advice calls.
            cgBuilder.buildCallGraph(AsmManager.getDefault());

            // System.err.println("Reach here!");
//            Map<AbstractMethodDeclaration, CallGraph> cgs = cgBuilder.getResult();
//            Initializer.out.writeLine("CGs Start:------------------------------------------------");
//            Initializer.out.writeLine("**********************************************************");
//            Initializer.out.writeCgs(cgs);
        } catch (Throwable e) {
            System.err.println("Caught unexpected exception in Call Graph Construction!");
            e.printStackTrace();
        }
    }
}
